<?php

 //文章模型

 Class articleModel extends Model{

     /**
      * 获得单条数据
      * @param $condition 条件
      * @return mixed
      */
       public function getArticleByOne($condition,$field){
              return $this->where($condition)->field($field)->find();
       }

     /**
      * 获得多条数据
      * @param $condition 条件
      * @param $field 字段
      * @param $order 排序
      * @param $limit 数量
      * @return mixed
      */
       public function getArticleByMore($condition,$field,$order,$limit){
              return $this->where($condition)->field($field)->order($order)->limit($limit)->select();
       }

       /**
        * 添加文章
        */
       public function addarticle($data){
             return $this->add($data);
       }

     /**
      * 修改
      * @param $condition 条件
      * @param $data 数据
      * @return mixed
      */
       public function updata($condition,$data){
             return $this->where($condition)->save($data);
       }



       /**
        * 删除文章
        */
       public function delectByid($id){

            return $this->delete($id);
       }

       /**
        * 前台首页数据
        */
       public function articListByPage($request){
            $page = $_GET['page']?$_GET['page']:1;
            $pageSize = 10;
            $startLimit = ($page-1)*$pageSize;
            $where = [];
            $arcdata = $this->getArticleByMore($where,'','',"$startLimit,$pageSize");
            foreach($arcdata as $k => $v){
                   $arcdata[$k]['pubname'] = M('admin_login')->where(array('id'=>$v['uid']))->getField('account');
                   $arcdata[$k]['cate'] = M("article_type")->where(array('id'=>$v['cid']))->getField('cname');
            }
            if(!$arcdata){
                  return $res = ['status'=>0,'msg'=>"数据已加载完毕"];
                  exit();
            }
            $data['pageNo'] = $page+1;
            $data['data'] = $arcdata;
            return $arcdata;
       }

       /**
        * 处理查看视图数据
        */
      public function processData($data){
           if(empty($data)) return;
//           $this->setFiledInc(array('id'=>$data['id']),'view');
                // 判断是否已经收藏
           $where['uid'] = session('uid');
           $where['fid'] = $data['id'];
           $is_collect = M("member_collection")->where($where)->find();
           $data['is_collect'] = $is_collect?1:0;
           return $data;
      }

      /**
       * 根据关键词 筛选 出相关文章
       */
      public function getRelationByKeyWord($keyword){
           // 中文分词
           $keywordArr = scws($keyword);
           // 随机选择三个词进行查询
           $randWord = [];
           for($i=0;$i<3;$i++){
               if(!in_array($keywordArr[array_rand($keywordArr)],$randWord))
                   $randWord[]= "%".trim($keywordArr[array_rand($keywordArr)])."%";
           }
           $where['title'] = array('like',$randWord,'OR');
           $relation = $this->getArticleByMore($where,'','',6);
           return $relation;
      }

     /**
      * 得到评论数据
      */
      public function getComment($id){
           $where['pid'] = $id;
           $data = D("Comment")->getCommentList($where,1);
           return $data;
      }




     /**
      *  处理字段增加减
      */
      public function setFiledInc($condition,$field){
            if($this->where($condition)->setInc($field,1)){
                 return true;
            }else{
                 return false;
            }
      }
 }

?>